﻿using FishServe.Core.Services.UpdaterService;
using FishServe.Core.Services.UpdaterService.Common;
using FishServe.Core.Services.UpdaterService.Interfaces;
using NUnit.Framework;
using FishServe.Core;
using System;

namespace Test.FishServe.Core.Services.UpdaterService
{
	//TODO: Refactor this test
	[TestFixture]
	public class ReleaseManifestServiceFixture
	{

		public const string MANIFEST_FILE_PATH_INVALID = @"C:\WorkDir\CEEDT\2008-12-31\Deployment\FishServe.Client.Solution.1.2.manifest";
		public const string MANIFEST_FILE_PATH = @"C:\WorkDir\CEEDT\2008-12-31\Deployment\FishServe.Client.Solution.1.3.manifest";

		private IReleaseManifestService releaseManifestService = new ReleaseManifestService();

		[Test]
		public void CanReadManifestFile()
		{
			ReleaseManifest releaseManifest =
			releaseManifestService.RetrieveReleaseManifestFrom(MANIFEST_FILE_PATH);

			Assert.IsNotEmpty(releaseManifest.ModuleManifests[0].DependentModules);
			Assert.IsTrue(releaseManifest.ModuleManifests.GetLength(0) == 7);
			Assert.IsNotNull(releaseManifest, "Failed to return ReleaseManifest object");
		}

		[Test]
		[ExpectedException(ExceptionType = typeof(AssertionFailedException))]
		public void CanotFindManifestFile()
		{
			ReleaseManifest releaseManifest =
				releaseManifestService.RetrieveReleaseManifestFrom(MANIFEST_FILE_PATH_INVALID);
		}

		[Test(Description = "Works:: Delete this test because does not belong in this fixture.")]
		public void ValidateReleaseManifest()
		{
			ReleaseManifest releaseManifest =
				releaseManifestService.RetrieveReleaseManifestFrom(MANIFEST_FILE_PATH);
			ReleaseManifestValidator validator = new ReleaseManifestValidator();
			bool result = validator.ValidateReleaseManifest(releaseManifest);

			Assert.IsTrue(result, "ReleaseManifest failed Validation");
		}


		/// <summary>
		/// Works... Delete this test later.
		/// </summary>
		[Test]
		public void TestBitFlagsOperation()
		{
			FileCheckTask fileCheckResult = new FileCheckTask()
				{
					FullFilePath = "FooBar.file",
					ResultState = Result.None
				};
			Console.WriteLine("ResultState:NONE");
			OutputResultState(fileCheckResult.ResultState);

			Assert.IsFalse(fileCheckResult.ResultState.ConstainsResult(Result.FilePresent), "Expecting FilePresent");
			Assert.IsFalse(fileCheckResult.ResultState.ConstainsResult(Result.FileHashPassed), "NOT Expecting FileHashPassed");
			Assert.IsFalse(fileCheckResult.ResultState.ConstainsResult(Result.FileAllDependenciesPresent));

			Console.WriteLine("Setting FileHashPassed to true");
			fileCheckResult.ResultState |= Result.FileHashPassed;
			OutputResultState(fileCheckResult.ResultState);
			Assert.IsTrue(fileCheckResult.ResultState.ConstainsResult(Result.FileHashPassed), "Expecting FileHashPassed");
			Assert.IsFalse(fileCheckResult.ResultState.ConstainsResult(Result.FilePresent), "Expecting FilePresent");
			Assert.IsFalse(fileCheckResult.ResultState.ConstainsResult(Result.FileAllDependenciesPresent));


			Console.WriteLine("Setting FilePresent to false.. Eventhough its already false to begin with");
			fileCheckResult.ResultState &= ~Result.FilePresent; //unsetting FilePresent bit.. Need to look into this.
			OutputResultState(fileCheckResult.ResultState);
			Assert.IsTrue(fileCheckResult.ResultState.ConstainsResult(Result.FileHashPassed), "Expecting FileHashPassed");
			Assert.IsFalse(fileCheckResult.ResultState.ConstainsResult(Result.FilePresent), "NOT Expecting FilePresent");
			Assert.IsFalse(fileCheckResult.ResultState.ConstainsResult(Result.FileAllDependenciesPresent));

			Console.WriteLine("Setting FilePresent to true.");
			fileCheckResult.ResultState |= Result.FilePresent;
			OutputResultState(fileCheckResult.ResultState);
			Assert.IsTrue(fileCheckResult.ResultState.ConstainsResult(Result.FileHashPassed), "Expecting FileHashPassed");
			Assert.IsTrue(fileCheckResult.ResultState.ConstainsResult(Result.FilePresent), "Expecting FilePresent");
			Assert.IsFalse(fileCheckResult.ResultState.ConstainsResult(Result.FileAllDependenciesPresent));


		}

		private void OutputResultState(Result resultState)
		{
			Console.WriteLine("FilePresent:{0}", resultState.ConstainsResult(Result.FilePresent));
			Console.WriteLine("FileHashPassed:{0}", resultState.ConstainsResult(Result.FileHashPassed));
			Console.WriteLine("FileHaveDependencies:{0}", resultState.ConstainsResult(Result.FileHaveDependencies));
			Console.WriteLine("FileAllDependenciesPresent:{0}", resultState.ConstainsResult(Result.FileAllDependenciesPresent));
			Console.WriteLine("--");
		}
	}
}
